When you have a CSV generated how do you test it runs. Take this extract:
public function export()
{ $actions = $this->getData()->get(); $columns = [ 'User', 'Start Date', 'End Date', ]; $data = []; foreach ($actions as $action) { $data[] = [ $action->user->name ?? '', $action->due_at, $action->completed_at, ]; } $now = Carbon::now()->format('d-m-Y'); $filename = "actions-{$now}"; retu csv_file($columns, $data, $filename);
}
The above collects data and sends it to a helper function called csv_file which in tu will export a CSV file.
For completeness it contains:
if (! function_exists('csv_file')) { function csv_file($columns, $data, string $filename = 'export'): BinaryFileResponse { $file = fopen('php://memory', 'wb'); $csvHeader = [...$columns]; fputcsv($file, $csvHeader); foreach ($data as $line) { fputcsv($file, $line); } fseek($file, 0); $uid = unique_id(); Storage::disk('local')->put("public/$uid", $file); retu response()->download(storage_path('app/public/'.$uid), "$filename.csv")->deleteFileAfterSend(true); }
}
Now there are 2 tests I want to confirm first, I get 200 status response to ensure the endpoint does not throw an error and secondly, the response contains the generated file name for Recent Questions...
ما را در سایت Recent Questions دنبال می کنید
برچسب : نویسنده : استخدام کار superuser بازدید : 211 تاريخ : چهارشنبه 22 تير 1401 ساعت: 15:59